package cn.ljy.uem.web.action.backstage.school;

import cn.ljy.authority.config.upload.FileUploadConfig;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.uem.model.SchoolCourseResource;
import cn.ljy.uem.model.query.SchoolCourseResourceQuery;
import cn.ljy.uem.service.SchoolCourseResourceService;
import cn.ljy.uem.utils.file.UemFileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * 学校课程资源模块
 * 说明：相应的课程资源必须是该课程的管理老师才能负责
 * @author 小锋
 * @date 2022/08/17
 */
@Api(tags = "学校课程资源模块")
@RestController
@RequestMapping(value = "/api/backstage/school/schoolCourseResource")
public class SchoolCourseResourceController extends BaseBackstageController<SchoolCourseResource, SchoolCourseResourceQuery, Integer> {

    @Autowired
    private SchoolCourseResourceService baseService;
    @Autowired
    private FileUploadConfig fileUploadConfig;
    @Autowired
    private UemFileUploadUtil uemFileUploadUtil;

    @ApiOperation(value = "查询所有课程资源")

    @GetMapping("/all")
    @Override
    protected ResponseData query(SchoolCourseResourceQuery query) {
        return super.query(query);
    }


    @ApiOperation(value = "分页查询课程资源")
    @Override
    public ResponseData query(Integer page, Integer pageSize, SchoolCourseResourceQuery query){
        return super.query(page,pageSize,query);
    }

    @ApiOperation(value = "读取课程资源信息")

    @Override
    protected ResponseData get(@PathVariable("id") Integer id) {
        return super.get(id);
    }

    @ApiOperation(value = "批量删除指定的多个课程资源")
    @Override
    protected ResponseData deletes(@NotEmpty(message = "操作失败：请选择要移除的记录") Integer[] ids) {
        return super.deletes(ids);
    }


    @ApiOperation(value = "添加课程资源")

    @Override
    protected ResponseData add(@Valid @RequestBody SchoolCourseResource schoolCourseResource) {
        return super.add(schoolCourseResource);
    }

    @GetMapping("/getResourceBySection/{SectionId}")
    @ApiOperation(value = "查找章节对应的课程资源")
    protected ResponseData getResourceBySection(@PathVariable String SectionId) {
        return ResponseData.success(baseService.GetAllCourseResourceBySectionId(SectionId));
    }

//    @GetMapping("/get/Curriculum/Resource")
//    @ApiOperation(value = "查找id对应的课程资源")
//    protected ResponseData getResourceBySection(@RequestBody SchoolCourseResource schoolCourseResource) {
//        List<SchoolCourseResource> schoolCourseResources=baseService.
//        return ResponseData.success(baseService.GetAllCourseResourceBySectionId(SectionId));
//    }


    @ApiOperation(value = "编辑课程资源")
    @Override
    protected ResponseData edit(@Valid @RequestBody SchoolCourseResource schoolCourseResource) {
        System.out.println(schoolCourseResource);
        return super.edit(schoolCourseResource);
    }
    /*============================定义开发===================================*/

    /**
     * ljy 20220823
     * 上传课程资源（半成品)
     * 特别说明：element ui的upload组件的批量上传默认是每个文件单独上传，并非一次上传所有,因为批量上传也可以使用本接口
     * @return
     */
    @ApiOperation(value = "非表单方式上传课程资源")
    @PostMapping("/upload")
    public ResponseData uploadResrouce(@NotNull(message="请选择课程")Long courseId,@NotNull(message="请选择上传文件") MultipartFile file) {
        return ResponseData.success(baseService.uploadResource(courseId, file));
    }

    /**
     * ljy 20220823
     * 批量上传课程资源（半成品)
     * 特别说明：
     * 1.element ui的upload组件的批量上传默认是每个文件单独上传，并非一次上传所有。
     * 2.如果真需要将前端设置为批量上传，则应考虑批量上传文件过程中有一个文件失败时，已上传成功文件的回滚（即删除问题），例如可以使用临时文件处理
     * @return
     */
    /*@ApiOperation(value = "批量上传课程资源")
    @PostMapping("/uploadBatch")
    public ResponseData uploadBatch(@NotNull(message="请选择课程")Long courseId,@NotNull(message="请选择上传文件") MultipartFile[] files) {
        //定义最终要返回的文件路径集合
        List<String> resourceUrls=new ArrayList<>();
        for(MultipartFile file:files){
            //上传资源，并将资源上传后的网址返回
            String resourceUrl=MyFileUpload.uploadMultipartFile(file,null,fileUploadConfig.getResourceUploadBasePath(),fileUploadConfig.getResourceUploadBaseUrl(),false,true);
            resourceUrls.add(resourceUrl);
        }
        //将上传的资源存进数据库
        //略
        return ResponseData.success(resourceUrls);
    }*/
}
